package com.laxcen.smoke.dao;

import com.laxcen.smoke.dto.res.UserMessageRes;
import com.laxcen.smoke.dto.res.UserResourceRes;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface UserDao {

    @Insert("insert into sys_user(f_id,f_username,f_account,f_password,f_salt,f_roleflag,f_onoroff,f_province,f_city,f_area,create_time) values(#{id},#{username},#{account},#{password},#{roleflag},#{onoroff},#{province},#{city},#{area},#{createtime})")
    int insert(String id, String username, String account, String password,String salt, int roleflag, int onoroff, String province, String city, String area, Date createtime);

    @Insert("insert into sys_user_role(user_id,role_id,create_time) values(#{userid},#{roleid},#{createtime})")
    int insertUserRole(String userid, long roleid, Date createtime);

    @Delete("delete from sys_user where f_id = #{id}")
    int delete(String id);

    @Delete("delete from sys_user_role where user_id = #{id}")
    int deleteUserAndRole(String id);

    @Update("update sys_user set f_username = #{username},f_account = #{account},f_password = #{password},f_salt=#{salt},f_roleflag = #{roleflag},f_onoroff = #{onoroff},f_province = #{province},f_city = #{city},f_area = #{area},f_updatetime = #{updatetime} where f_id = #{id}")
    int update(String id, String username, String account, String password,String salt, int roleflag, int onoroff, String province, String city, String area, Date updatetime);

    @Select("select f_id id,f_username username,f_account account,f_roleflag roleflag,f_onoroff onoroff,f_province province,f_city city,f_area area,create_time createtime,last_login_time lastlogintime,login_count logincount from sys_user")
    List<UserMessageRes> getUserList();

    @Select("select f_id id,f_username username,f_account account,f_roleflag roleflag,f_onoroff onoroff,f_province province,f_city city,f_area area,create_time createtime ,is_root,last_login_time lastlogintime,login_count logincount from sys_user where f_account = #{account} ")
    UserMessageRes getUserOne(String account);

    @Select("select f_account from sys_user ")
    List<String>  getAccountAll();

    @Select("select f_account from sys_user where F_OnOrOff = #{onoroff}")
    List<String>  getAccountOn(int onoroff);

    @Select("select f_username from sys_user")
    List<String>  getUsernameAll();

    @Select("select f_password from sys_user where f_account = #{account} and F_OnOrOff = #{onoroff}")
    String getPassByAcco(String account, int onoroff);

    @Select("select f_password from sys_user where f_account = #{account} ")
    String getPassByAccoAll(String account);

    //根据用户账号查询用户名
    @Select("select f_username from sys_user where f_account = #{account}")
    String  getUsernameByAccount(String account);

    @Select("select f_account from sys_user where f_id <> #{id}")
    List<String>  getAccountByOne(String id);

    @Select("select f_username from sys_user where f_id <> #{id}")
    List<String>  getUsernameByOne(String id);

    @Select("select f_id id,f_username username,f_account account,f_roleflag roleflag,f_onoroff onoroff,f_province province,f_city city,f_area area,create_time createtime,last_login_time lastlogintime,login_count logincount from sys_user where f_account like CONCAT('%',#{account},'%') ")
    List<UserMessageRes> getListByAccount(String account);

    @Select("select f_id id,f_username username,f_account account,f_roleflag roleflag,f_onoroff onoroff,f_province province,f_city city,f_area area,create_time createtime,last_login_time lastlogintime,login_count logincount from sys_user where f_username like CONCAT('%',#{username},'%')  ")
    List<UserMessageRes> getListByUSername(String username);

    @Select("select f_id id,f_username username,f_account account,f_roleflag roleflag,f_onoroff onoroff,f_province province,f_city city,f_area area,create_time createtime,last_login_time lastlogintime,login_count logincount from sys_user where f_account like CONCAT('%',#{account},'%') and f_username like CONCAT('%',#{username},'%') ")
    List<UserMessageRes> getListByUSernameAndAccount(String account, String username);

    @Select("select f_id id,f_username username,f_account account,f_roleflag roleflag,f_onoroff onoroff,,f_province province,f_city city,f_area area,create_time createtime,last_login_time lastlogintime,login_count logincount from sys_user where createtime between #{starttime} and #{endtime}")
    List<UserMessageRes> getListByTime(Date starttime, Date endtime);

    @Select("select f_id id,f_username username,f_account account,f_roleflag roleflag,f_onoroff onoroff,,f_province province,f_city city,f_area area,create_time createtime,last_login_time lastlogintime,login_count logincount from sys_user where f_account = #{account} and createtime between #{starttime} and #{endtime}")
    List<UserMessageRes> getListByTimeAndAcc(String account, Date starttime, Date endtime);

    @Select("SELECT re.id,re.`name`,re.parent_id,re.url,re.permission,re.sort FROM sys_resources re left JOIN sys_role_resources rr ON re.id = rr.resources_id left JOIN sys_user_role ur ON rr.role_id = ur.role_id WHERE ur.user_id = #{userid} AND re.available = 1 ORDER BY re.parent_id ASC,re.sort ASC")
    List<UserResourceRes> getUserResource(String userid);

}
